package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
   @Autowired
   private PermissionMapper permissionMapper;

   //添加权限
   public void add(Permission permission) {
      permissionMapper.add(permission);
   }

   //分页查询
   public PageResult findPage(QueryPageBean queryPageBean) {
      //1.获取参数
      Integer currentPage = queryPageBean.getCurrentPage();
      Integer pageSize = queryPageBean.getPageSize();
      String queryString = queryPageBean.getQueryString();
      //2.使用分页助手
      PageHelper.startPage(currentPage, pageSize);
      //3.调用条件查询方法
      Page<Permission> page = permissionMapper.findByCondition(queryString);
      //4.返回结果
      return new PageResult(page.getTotal(),page.getResult());
   }

   //通过id删除权限
   public void deleteById(Integer id) {
      //1.删除权限角色关系
      permissionMapper.deletePermissionAndRoleRel(id);
      //2.删除权限
      permissionMapper.deleteById(id);
   }

   //通过id 查询权限数据
   public Permission findById(Integer id) {
      Permission permission = permissionMapper.findById(id);
      return permission;
   }

   //修改权限
   public void edit(Permission permission) {
      permissionMapper.edit(permission);
   }
}
